package com.pm.business.service.impl;

import java.util.List;
import com.pm.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.pm.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.pm.business.domain.CompeteEnterpriseProjectAward;
import com.pm.business.mapper.CompeteEnterpriseHistoryProjectMapper;
import com.pm.business.domain.CompeteEnterpriseHistoryProject;
import com.pm.business.service.ICompeteEnterpriseHistoryProjectService;

/**
 * 历史业绩Service业务层处理
 * 
 * @author zxl
 * @date 2022-05-07
 */
@Service
public class CompeteEnterpriseHistoryProjectServiceImpl implements ICompeteEnterpriseHistoryProjectService 
{
    @Autowired
    private CompeteEnterpriseHistoryProjectMapper competeEnterpriseHistoryProjectMapper;

    /**
     * 查询历史业绩
     * 
     * @param projectId 历史业绩主键
     * @return 历史业绩
     */
    @Override
    public CompeteEnterpriseHistoryProject selectCompeteEnterpriseHistoryProjectByProjectId(Long projectId)
    {
        return competeEnterpriseHistoryProjectMapper.selectCompeteEnterpriseHistoryProjectByProjectId(projectId);
    }

    /**
     * 查询历史业绩列表
     * 
     * @param competeEnterpriseHistoryProject 历史业绩
     * @return 历史业绩
     */
    @Override
    public List<CompeteEnterpriseHistoryProject> selectCompeteEnterpriseHistoryProjectList(CompeteEnterpriseHistoryProject competeEnterpriseHistoryProject)
    {
        return competeEnterpriseHistoryProjectMapper.selectCompeteEnterpriseHistoryProjectList(competeEnterpriseHistoryProject);
    }

    /**
     * 新增历史业绩
     * 
     * @param competeEnterpriseHistoryProject 历史业绩
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCompeteEnterpriseHistoryProject(CompeteEnterpriseHistoryProject competeEnterpriseHistoryProject)
    {
        competeEnterpriseHistoryProject.setCreateTime(DateUtils.getNowDate());
        int rows = competeEnterpriseHistoryProjectMapper.insertCompeteEnterpriseHistoryProject(competeEnterpriseHistoryProject);
        insertCompeteEnterpriseProjectAward(competeEnterpriseHistoryProject);
        return rows;
    }

    /**
     * 修改历史业绩
     * 
     * @param competeEnterpriseHistoryProject 历史业绩
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCompeteEnterpriseHistoryProject(CompeteEnterpriseHistoryProject competeEnterpriseHistoryProject)
    {
        competeEnterpriseHistoryProject.setUpdateTime(DateUtils.getNowDate());
        competeEnterpriseHistoryProjectMapper.deleteCompeteEnterpriseProjectAwardByProjectId(competeEnterpriseHistoryProject.getProjectId());
        insertCompeteEnterpriseProjectAward(competeEnterpriseHistoryProject);
        return competeEnterpriseHistoryProjectMapper.updateCompeteEnterpriseHistoryProject(competeEnterpriseHistoryProject);
    }

    /**
     * 批量删除历史业绩
     * 
     * @param projectIds 需要删除的历史业绩主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCompeteEnterpriseHistoryProjectByProjectIds(Long[] projectIds)
    {
        competeEnterpriseHistoryProjectMapper.deleteCompeteEnterpriseProjectAwardByProjectIds(projectIds);
        return competeEnterpriseHistoryProjectMapper.deleteCompeteEnterpriseHistoryProjectByProjectIds(projectIds);
    }

    /**
     * 删除历史业绩信息
     * 
     * @param projectId 历史业绩主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCompeteEnterpriseHistoryProjectByProjectId(Long projectId)
    {
        competeEnterpriseHistoryProjectMapper.deleteCompeteEnterpriseProjectAwardByProjectId(projectId);
        return competeEnterpriseHistoryProjectMapper.deleteCompeteEnterpriseHistoryProjectByProjectId(projectId);
    }

    /**
     * 新增竞争单位历史项目获奖信息
     * 
     * @param competeEnterpriseHistoryProject 历史业绩对象
     */
    public void insertCompeteEnterpriseProjectAward(CompeteEnterpriseHistoryProject competeEnterpriseHistoryProject)
    {
        List<CompeteEnterpriseProjectAward> competeEnterpriseProjectAwardList = competeEnterpriseHistoryProject.getCompeteEnterpriseProjectAwardList();
        Long projectId = competeEnterpriseHistoryProject.getProjectId();
        if (StringUtils.isNotNull(competeEnterpriseProjectAwardList))
        {
            List<CompeteEnterpriseProjectAward> list = new ArrayList<CompeteEnterpriseProjectAward>();
            for (CompeteEnterpriseProjectAward competeEnterpriseProjectAward : competeEnterpriseProjectAwardList)
            {
                competeEnterpriseProjectAward.setProjectId(projectId);
                list.add(competeEnterpriseProjectAward);
            }
            if (list.size() > 0)
            {
                competeEnterpriseHistoryProjectMapper.batchCompeteEnterpriseProjectAward(list);
            }
        }
    }
}
